package com.shenzhenai.composebasic.components

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.TweenSpec
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowBackIosNew
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.FabPosition
import androidx.compose.material3.Icon
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCompositionContext
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import kotlin.math.abs
import kotlin.math.roundToInt

/**
 * @author huangzehui
 * @date 14/9/2025 下午11:16 周日
 * @description
 *
 *
 * Scaffold 使用1
 *
 * https://www.bilibili.com/video/BV1JR4y1G7LU?vd_source=e07cc8cc94eb22bd76722071b03cd199&spm_id_from=333.788.player.switch
 */


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScaffoldSample() {
    val scaffoldState = rememberBottomSheetScaffoldState()

    val sharpEdgePercent = -50f
    val roundEdgePercent = 45f

    val animatedProgress = remember {
        Animatable(sharpEdgePercent)
    }

    val coroutineScope = rememberCoroutineScope()
    val progress = animatedProgress.value.roundToInt()

    val fabShape = when {
        progress < 0 -> {
            CutCornerShape(abs(progress))
        }

        progress == roundEdgePercent.toInt() -> {
            CircleShape
        }

        else -> {
            RoundedCornerShape(progress)
        }
    }

    val changeShape: () -> Unit = {
        val target = animatedProgress.targetValue
        val nextTarget = if (target == roundEdgePercent) {
            target
        } else {
            target
        }

        coroutineScope.launch {
            animatedProgress.animateTo(
                targetValue = nextTarget,
                animationSpec = TweenSpec(durationMillis = 1000)
            )
        }
    }

    Scaffold(
        topBar = {
//        Text(text = "Title")
            TopAppBar(
                title = {
                    Text(text = "Title")
                },
                navigationIcon = {
                    Icon(
                        imageVector = Icons.Default.ArrowBackIosNew,
                        contentDescription = null
                    )
                },
                actions = {
                    Icon(
                        imageVector = Icons.Default.Add,
                        contentDescription = null
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Text(text = "Edit")
                },
                colors = TopAppBarDefaults.topAppBarColors().copy(
                    // 容器颜色
                    containerColor = Color.Green
                )
            )
        },

        bottomBar = {
            BottomAppBar(actions = {})
        },

        floatingActionButton = {
            ExtendedFloatingActionButton(onClick = changeShape, shape = fabShape) {
                Text(text = "Change Shape")
            }
        },

        floatingActionButtonPosition = FabPosition.Center,


        ) {
        Box(modifier = Modifier.padding(it)) {
            Text(text = "Scaffold Body Content")
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScaffoldSample1() {
    // 1. 创建抽屉状态和控制协程作用域
    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
    val scope = rememberCoroutineScope()

    // 2. 使用 ModalNavigationDrawer 包裹 Scaffold
    ModalNavigationDrawer(
        drawerState = drawerState,
        // drawerContent 参数在这里！
        drawerContent = {
            // 这里是你的抽屉内容
           Column {
               Text("抽屉项 1")
               Text("抽屉项 2")
           }
            // 可以添加更多内容，如列表、头像等
        },
        gesturesEnabled = true, // 启用手势滑动打开抽屉
    ) {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = {
                        Text(text = "Title")
                    },
                    navigationIcon = {
                        Icon(
                            imageVector = Icons.Default.ArrowBackIosNew,
                            contentDescription = null,
                            modifier = Modifier.clickable {
                                scope.launch {
                                    drawerState.open()
                                }
                            }
                        )
                    },
                    actions = {
                        Icon(
                            imageVector = Icons.Default.Add,
                            contentDescription = null
                        )
                        Spacer(modifier = Modifier.width(8.dp))
                        Text(text = "Edit")
                    },
                    colors = TopAppBarDefaults.topAppBarColors().copy(
                        // 容器颜色
                        containerColor = Color.Green
                    )
                )
            },

            bottomBar = {

            },

            ) {
            it
        }
    }

}

@Preview
@Composable
fun ScaffoldSamplePreview() {
//    ScaffoldSample()
    ScaffoldSample1()
}